package com.sunzm.spark.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object ScalaWordCount {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("RDD wordcount示例")
      .setMaster("local[*]")
    val sc: SparkContext = new SparkContext(conf)

    val dataRDD: RDD[String] = sc.textFile("data/spark/rdd/word.txt")

    /*val wordRDD: RDD[Array[String]] = dataRDD.map(line => {
      /* val wordArray: Array[String] = line.split(",")
      wordArray*/
      line.split(",")
    })*/


   /* val wordRDD: RDD[String] = dataRDD.flatMap(line => {
      line.split(",")
    })

    val result = wordRDD.map((_, 1)).reduceByKey(_ + _)*/

    val result: RDD[(String, Int)] = dataRDD.flatMap(_.split(",")).map((_, 1)).reduceByKey(_ + _)

    result.foreach(println(_))

    sc.stop()
  }
}
